//
//  DaysTrainingPlanViewController.m
//  JiZhuiAn
//
//  Created by Easy.Lin on 16/6/27.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "DaysTrainingPlanViewController.h"
#import "PlayVideoViewController.h"
#import "LastingDaysView.h"
#import "TrainingDescriptionView.h"
#import "TrainingProjectData.h"
#import "Node.h"

@interface DaysTrainingPlanViewController ()<StartTrainingDelegate,TrainingDescriptionDelegate>

@property (nonatomic, strong) DaysTrainingPlanViewModel * viewModel;
@property (nonatomic, strong) LastingDaysView * lastingDaysView;
@property (nonatomic, strong) TrainingDescriptionView * trainingDescriptionView;
@end

@implementation DaysTrainingPlanViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewSetting
{
    [super viewSetting];
    [self.view addSubview:self.lastingDaysView];
    [self.view addSubview:self.trainingDescriptionView];
}

- (void)bindViewModel
{
    [super bindViewModel];
    [self.viewModel.getTrainingProjectCommand execute:nil];
    @weakify(self);
    [RACObserve(self.viewModel, duration) subscribeNext:^(NSNumber * x) {
        @strongify(self);
        self.lastingDaysView.lastingDayImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"day_%zd",[x integerValue]]];
    }];
    [RACObserve(self.viewModel, trainingDay) subscribeNext:^(NSNumber * x) {
        @strongify(self);
        if ([x integerValue] > 0) {
            self.title = [NSString stringWithFormat:@"第%zd天锻炼计划",[x integerValue]];
            NSString * dayString = [[NSString alloc] init];
            switch ([x integerValue]) {
                case 1:
                    dayString = @"一";
                    break;
                case 2:
                    dayString = @"二";
                    break;
                case 3:
                    dayString = @"三";
                    break;
                case 4:
                    dayString = @"四";
                    break;
                case 5:
                    dayString = @"五";
                    break;
                case 6:
                    dayString = @"六";
                    break;
                case 7:
                    dayString = @"七";
                    break;
                default:
                    break;
            }
            self.trainingDescriptionView.dayLabel.text = [NSString stringWithFormat:@"第%@天",dayString];
        }
    }];
    [[RACObserve(self.viewModel, trainingProjectArray) skip:1] subscribeNext:^(NSArray * x) {
        @strongify(self);
        TrainingProjectData * each_1 = [x objectAtIndex:0];
        TrainingProjectData * each_2 = [x objectAtIndex:1];
        TrainingProjectData * each_3 = [x objectAtIndex:2];
        TrainingProjectData * each_4 = [x objectAtIndex:3];
        [self.trainingDescriptionView.firstMoveButton setTitle:each_1.projectDescription forState:UIControlStateNormal];
        [self.trainingDescriptionView.secondMoveButton setTitle:each_2.projectDescription forState:UIControlStateNormal];
        [self.trainingDescriptionView.thirdMoveButton setTitle:each_3.projectDescription forState:UIControlStateNormal];
        [self.trainingDescriptionView.fourthMoveButton setTitle:each_4.projectDescription forState:UIControlStateNormal];
    }];
}

- (void)makeConstaints
{
    [super makeConstaints];
    WS(weakSelf);
    [self.lastingDaysView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@(screenHeight/3));
    }];
    [self.trainingDescriptionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.top.equalTo(weakSelf.lastingDaysView.mas_bottom);
    }];
}

#pragma mark - StartTrainingDayDelegate
- (void)startTraining
{
    [self selectedMove:1];
}

#pragma mark - TrainingDescriptionDelegate
- (void)selectedMove:(NSInteger)moveTag
{
    TrainingProjectData * data = [self.viewModel.trainingProjectArray objectAtIndex:moveTag - 1];
    Node * node = [[Node alloc] init];
    node.data = data;
    PlayVideoViewModel * viewModel = [[PlayVideoViewModel alloc] init];
    viewModel.targetNode = node;
    PlayVideoViewController * vc = [[PlayVideoViewController alloc] initWithViewModel:viewModel];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Getter
- (LastingDaysView *)lastingDaysView
{
    if (!_lastingDaysView) {
        _lastingDaysView = [[LastingDaysView alloc] initWithLastingDay:self.viewModel.trainingDay];
        _lastingDaysView.delegate = self;
    }
    return _lastingDaysView;
}

- (TrainingDescriptionView *)trainingDescriptionView
{
    if (!_trainingDescriptionView) {
        _trainingDescriptionView = [[TrainingDescriptionView alloc] init];
        _trainingDescriptionView.delegate = self;
    }
    return _trainingDescriptionView;
}

#pragma mark - dealloc
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
